package patrol.mobile.com.mobilepatrol.activity.culvert;

import android.content.Intent;
import android.os.Vibrator;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.orhanobut.logger.Logger;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import cn.bingoogolapple.qrcode.core.QRCodeView;
import cn.bingoogolapple.qrcode.zxing.ZXingView;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.BaseActivity;
import patrol.mobile.com.mobilepatrol.api.Urls;
import patrol.mobile.com.mobilepatrol.bean.Handong;
import patrol.mobile.com.mobilepatrol.manager.HManager;
import patrol.mobile.com.mobilepatrol.manager.UserManager;
import patrol.mobile.com.mobilepatrol.persenter.QHMesPer;
import patrol.mobile.com.mobilepatrol.utils.CommonUtil;
import patrol.mobile.com.mobilepatrol.utils.Constants;
import patrol.mobile.com.mobilepatrol.utils.GsonUtil;
import patrol.mobile.com.mobilepatrol.utils.NetUtil;
import patrol.mobile.com.mobilepatrol.utils.SPUtils;
import patrol.mobile.com.mobilepatrol.view.QHMesView;

public class CulvertScanActivity extends BaseActivity implements QRCodeView.Delegate,QHMesView {
    private static final String TAG = CulvertScanActivity.class.getSimpleName();
    public QRCodeView mQRCodeView;
    private String hdcode;

    @Override
    protected void onStart() {
        super.onStart();
        // 打开后置摄像头开始预览，但是并未开始识别
        mQRCodeView.startCamera();
        //显示扫描框
        mQRCodeView.showScanRect();
        //开始扫描
        mQRCodeView.startSpot();
    }

    @Override
    protected void onStop() {
        //关闭摄像头预览，并且隐藏扫描框
        mQRCodeView.stopCamera();
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        mQRCodeView.onDestroy();
        super.onDestroy();
    }


    @Override
    public void onScanQRCodeSuccess(String result) {
        vibrate();

        String[]  strs =result.split("&");
        hdcode = strs[0].substring(strs[0].indexOf("=")+1);
        if (NetUtil.isNetworkAvailable(CulvertScanActivity.this)){
            Map<String,String> map = new HashMap<>();
            map.put("hdcode",hdcode);
            map.put("userName", UserManager.getUser((String) SPUtils.get(Constants.current_id, "")).username);
            map.put("passWord", UserManager.getUser((String) SPUtils.get(Constants.current_id, "")).pw);
            QHMesPer qhMesPer = new QHMesPer(CulvertScanActivity.this, map);
            qhMesPer.getQHMes(Urls.queryHd);
        }else{
            if (HManager.isExit(hdcode)==null){
                showToast("没有网络");
        }else{
            Intent intent = new Intent(CulvertScanActivity.this,CulvertDetailActivity.class);
            intent.putExtra("hdcode",hdcode);
            startActivity(intent);
            CommonUtil.openNewActivityAnim(CulvertScanActivity.this,true);
        }
        }
//



        mQRCodeView.startSpot();

    }

    @Override
    public void onScanQRCodeOpenCameraError() {
        Toast.makeText(CulvertScanActivity.this,"打开相机出错", Toast.LENGTH_SHORT).show();
        Log.e(TAG, "打开相机出错");
        finish();
    }
    @Override
    public void initTitle() {
        iv_back.setImageResource(R.mipmap.up_white);
        iv_back.setVisibility(View.VISIBLE);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CommonUtil.exitActivityAndBackAnim(CulvertScanActivity.this,true);
            }
        });
        tv_center_title.setVisibility(View.VISIBLE);
        tv_center_title.setText("扫描二维码");
    }

    @Override
    public void initView() {
        super.initView();
        mQRCodeView = (ZXingView) findViewById(R.id.zxingview);
        //设置扫描二维码的代理
        mQRCodeView.setDelegate(this);
    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_scan;
    }

    @Override
    public void setListener() {

    }

    //震动
    private void vibrate() {
        Vibrator vibrator = (Vibrator)getSystemService(VIBRATOR_SERVICE);
        vibrator.vibrate(200);
    }

    @Override
    public void onQHError(String e) {
        showToast(e);
    }

    @Override
    public void onQHSuccess(String s) {
        Logger.d(s);
        try {
            JSONObject object = new JSONObject(s);
            if (object.getInt("state") == 200) {
                JSONObject obj = object.getJSONObject("result");
                final JSONArray hds = obj.getJSONArray("hd");
                if (hds==null||hds.length()==0){
                    showToast("没有相关涵洞信息！");
                    return;
                }
                final Handong hd = GsonUtil.GsonToBean(hds.get(0).toString(),Handong.class);
                //如果有定期，或者经常列表，就初始化桥梁isUp = true
                if ((hd.dqjc!=null&&hd.dqjc.size()>0)||(hd.jcxjc!=null&&hd.jcxjc.size()>0)){
                    hd.isUp = true;
                }

                if (HManager.isExit(hdcode)==null){
                    HManager.insertHD(hd);
                }else{
                    //删除桥梁
                    //定期，定期部件，定期缺损，定期图片,经常，经常缺损
                    HManager.delHD(hd);
                    HManager.insertHD(hd);

                }


                //初始化定期列表isUp= true
                if (hd.dqjc!=null&&hd.dqjc.size()>0){
                    for (int i=0;i<hd.dqjc.size();i++){
                        HManager.setHDDQListUpState(hd.dqjc.get(i).hdcode,true);
                    }

                }
                //初始化经常列表isUp= true
                if (hd.jcxjc!=null&&hd.jcxjc.size()>0){
                    for (int i=0;i<hd.jcxjc.size();i++){
                        HManager.setHDJCListUpState(hd.jcxjc.get(i).hdcode,true);
                    }

                }
                Intent intent = new Intent(CulvertScanActivity.this,CulvertDetailActivity.class);
                intent.putExtra("hdcode",hdcode);
                startActivity(intent);
                CommonUtil.openNewActivityAnim(CulvertScanActivity.this,true);
            } else {
                hideLoading();
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}